package com.doyled.jump.object;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.image.BufferedImage;

import com.doyled.jump.IGame.GameInterface;
import com.doyled.jump.framework.ResourceManager;
import com.doyled.jump.util.ChessKind;
import com.doyled.jump.util.Drawing;
import com.doyled.jump.util.HorseState;

public class Horsemen implements GameInterface{
	private HorseState state;
	private Spot defaultSpot;
	private Spot spot;
	private BufferedImage image;
	private ChessKind kind;
	private int width, height;
	private boolean visible;
	
	public boolean isVisible() {
		return visible;
	}

	public void setVisible(boolean visible) {
		this.visible = visible;
	}

	public Horsemen(ChessKind kind, Spot spot){
		super();
		this.visible = false;
		this.state = HorseState.InFarm;
		this.kind = kind;
		this.image = getHorseImage(kind);
		this.spot = spot;
		this.defaultSpot = spot;
		this.width = image.getWidth();
		this.height = image.getHeight();
	}
	
	public Spot getSpot() {
		return spot;
	}

	public void setSpot(Spot spot) {
		this.spot = spot;
	}

	public ChessKind getKind() {
		return kind;
	}

	public void setKind(ChessKind kind) {
		this.kind = kind;
	}

	public HorseState getState() {
		return state;
	}

	public void setState(HorseState state) {
		this.state = state;
	}

	private BufferedImage getHorseImage(ChessKind kind){
		switch (kind) {
		case Red:
			return ResourceManager.getInstance().getHorsemenRedImage();
		case Blue:
			return ResourceManager.getInstance().getHorsemenBlueImage();
		case Yellow:
			return ResourceManager.getInstance().getHorsemenYellowImage();
		case Green:
			return ResourceManager.getInstance().getHorsemenGreenImage();
		}
		return ResourceManager.getInstance().getHorsemenImage();
	}
	
	public boolean isOnHorse(Point mouseClick){
		int mouseX = (int)mouseClick.getX();
		int mouseY = (int)mouseClick.getY();
		int x = this.spot.getX();
		int y = this.spot.getY();
		int halfW = width / 2;
		int halfH = height / 2;
		return (mouseX >= x - halfW && mouseX <= x + halfW)
				&& (mouseY >= y- halfH && mouseY <= y + halfH);
	}
	
	public void beKickedToFarm(){
		setSpot(defaultSpot);
		setState(HorseState.InFarm);
	}
	
	@Override
	public void tick() {
		
	}

	@Override
	public void render(Graphics g) {
		if(visible){
			Drawing.draw(g, image, spot.getX(), spot.getY());
		}
	}
	
	
}
